package org.usmile.algorithms.huawei.acm;

import java.util.*;

// https://blog.csdn.net/qfc_128220/article/details/128856583
public class _工单调度策略 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int[][] works = new int[n][2];
        for (int i = 0; i < n; i++) {
            works[i][0] = sc.nextInt();
            works[i][1] = sc.nextInt();
        }

        System.out.println(getResult(n, works));
    }

    public static int getResult(int n, int[][] works) {
        Arrays.sort(works, Comparator.comparingInt(a -> a[0]));
        PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a));

        int curTime = 0;
        int ans = 0;
        for (int[] work : works) {
            int endTime = work[0];
            int score = work[1];

            if (endTime >= curTime + 1) {
                pq.offer(score);
                ans += score;

                curTime++;
            } else {
                if (pq.size() == 0) {
                    continue;
                }

                int minScore = pq.peek();
                if (score > minScore) {
                    pq.poll();
                    pq.offer(score);
                    ans += score - minScore;
                }
            }
        }

        return ans;
    }
}
